﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  
  <head>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-39155502-5"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
  
      gtag('config', 'UA-39155502-5');
    </script>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Global filters | MongoDB.Entities </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="Global filters | MongoDB.Entities ">
    <meta name="generator" content="docfx 2.58.0.0">
    <meta name="description" content="A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management.">
    <link rel="shortcut icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../styles/docfx.css">
    <link rel="stylesheet" href="../styles/main.css">
    <meta property="docfx:navrel" content="../toc.html">
    <meta property="docfx:tocrel" content="toc.html">
    
    <meta property="docfx:rel" content="../">
    <meta property="docfx:newtab" content="true">
    <meta property="og:title" content="MongoDB.Entities">
    <meta property="og:site_name" content="MongoDB.Entities">
    <meta property="og:url" content="https://mongodb-entities.com">
    <meta property="og:description" content="A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management,">
    <meta property="og:type" content="website">
    <meta property="og:image" content="https://mongodb-entities.com/images/social-square.png">  
  </head>
  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              
              <a class="navbar-brand" href="../index.html">
                <img id="logo" class="svg" src="../images/icon.png" alt="">
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
            </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div class="container body-content">
        
        <div id="search-results">
          <div class="search-list">Search Results for <span></span></div>
          <div class="sr-items">
            <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
          </div>
          <ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul>
        </div>
      </div>
      <div role="main" class="container body-content hide-when-search">
        
        <div class="sidenav hide-when-search">
          <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
          <div class="sidetoggle collapse" id="sidetoggle">
            <div id="sidetoc"></div>
          </div>
        </div>
        <div class="article row grid-right">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="">
<h1 id="global-filters">Global filters</h1>

<p>with the use of global filters you can specify a set of criteria to be applied to all operations performed by a <code>DBContext</code> instance in order to save the trouble of having to specify the same criteria in each and every operation you perform. i.e. you specify common criteria in one place, and all <strong>retrieval, update and delete</strong> operations will have the common filters automatically applied to them before execution.</p>
<p>to be able to specify common criteria, you need to create a derived <code>DBContext</code> class just like with the event hooks.</p>
<pre><code class="lang-csharp">public class MyDBContext : DBContext
{
    public MyDBContext()
    {
        SetGlobalFilter&lt;Book&gt;(
            b =&gt; b.Publisher == &quot;Harper Collins&quot; &amp;&amp;
                 b.IsDeleted == false);

        SetGlobalFilter&lt;Author&gt;(
            a =&gt; a.Status == &quot;Active&quot; &amp;&amp;
                 a.IsDeleted == false);
    }
}
</code></pre>
<h2 id="specify-filters-using-a-base-class">Specify filters using a base class</h2>
<p>filters can be specified on a per entity type basis like above or common filters can be specified using a base class type like so:</p>
<pre><code class="lang-csharp">SetGlobalFilterForBaseClass&lt;BaseEntity&gt;(x =&gt; x.IsDeleted == false);
</code></pre>
<h2 id="specify-filters-using-an-interface">Specify filters using an interface</h2>
<p>if you'd like a global filter to be applied to any entity type that implements an interface, you can specify it like below using a json string.
it is currently not possible to do it in a strongly typed manner due to a limitation in the driver.</p>
<pre><code class="lang-csharp">SetGlobalFilterForInterface&lt;ISoftDeletable&gt;(&quot;{ IsDeleted : false }&quot;);
</code></pre>
<h2 id="prepending-global-filters">Prepending global filters</h2>
<p>global filters by deafult are appended to your operation filters. if you'd like to instead have the global filters prepended, use the following overload:</p>
<pre><code class="lang-csharp">SetGlobalFilter&lt;Book&gt;(
    filter: b =&gt; b.Publisher == &quot;Harper Collins&quot;,
    prepend: true);
</code></pre>
<h2 id="temporarily-ignoring-global-filters">Temporarily ignoring global filters</h2>
<p>it's possible to skip/ignore global filters on a per operation basis as follows:</p>
<pre><code class="lang-csharp">//with command builders:
await db.Find&lt;Book&gt;()
        .Match(b =&gt; b.Title == &quot;Power Of Tomorrow&quot;)
        .IgnoreGlobalFilters()
        .ExecuteAsync();

//with direct methods:
await db.DeleteAsync&lt;Book&gt;(
    b =&gt; b.Title == &quot;Power Of Tomorrow&quot;,
    ignoreGlobalFilters: true);
</code></pre>
<h2 id="limitations">Limitations</h2>
<ol>
<li><p>only one filter per entity type is allowed. specify multiple criteria for the same entity type with the <code>&amp;&amp;</code> operator as shown above. if you call <code>SetGlobalFilter&lt;Book&gt;</code> more than once, only the last call will be registered.</p>
</li>
<li><p>if using a base class to specify filters, no derived entity type (of that specific base class) can be used for registering another filter. take the following for example:</p>
</li>
</ol>
<pre><code class="lang-csharp">    SetGlobalFilter&lt;Book&gt;(b =&gt; b.Publisher == &quot;Harper Collins&quot;);

    SetGlobalFilterForBaseClass&lt;BaseEntity&gt;(x =&gt; x.IsDeleted == false);
</code></pre>
<p>only the second filter would take effect. the first one is discarded because the <code>Book</code> type is a derived type of <code>BaseEntity</code>.</p>
<p>you can however switch the order of registration so that the base class registration occurs first. but you need to make sure to include the criteria the base class registration caters to as well, like so:</p>
<pre><code class="lang-csharp">    SetGlobalFilterForBaseClass&lt;BaseEntity&gt;(x =&gt; x.IsDeleted == false);

    SetGlobalFilter&lt;Book&gt;(
          b =&gt; b.Publisher == &quot;Harper Collins&quot; &amp;&amp;
               b.IsDeleted == false);
</code></pre>
<ol start="3">
<li>only delete, update and retrieval operations will use global filters. the <code>Save*()</code> operations will ignore any registered global filters as they will only match on the <code>ID</code> field.</li>
</ol>
</article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
                <h5>In This Article</h5>
                <div></div>
              </nav>
            </div>
          </div>
        </div>
      </div>
      
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            Developed by <a href='https://github.com/dj-nitehawk'>Đĵ ΝιΓΞΗΛψΚ</a> and <a href='https://github.com/dj-nitehawk/MongoDB.Entities/graphs/contributors'>contributors</a> / Licensed under <a href='https://github.com/dj-nitehawk/MongoDB.Entities/blob/master/LICENSE'>MIT</a> / Website generated by <a href='https://dotnet.github.io/docfx/'>DocFX</a>
            
          </div>
        </div>
      </footer>
    </div>
    
    <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../styles/docfx.js"></script>
    <script type="text/javascript" src="../styles/main.js"></script>
  </body>
</html>
